<?php
/**
 * Created by PhpStorm.
 * User: dell
 * Date: 2019/3/22
 * Time: 11:02
 */
namespace Modules\Service\Logic;
use App\Models\Advs;
use App\Models\Attachments;
use App\Models\Banner;
use App\Models\Configure;
use App\Models\Goods;
use App\Models\Nav;
use App\Models\News;
use App\Models\Notice;
use App\Models\Recharge;
use function foo\func;
use Illuminate\Support\Facades\Log;

class HomeLogic extends BaseLogic{

    const PATH = "http://api.gushitech.com/v2/";
    //首页信息
    public static function home($platform,$version){
        try {
            //查询轮播
            $banner = Advs::advsImage('banner');
            //获取公告
            $notice = Notice::orderBy('id', 'desc')->get();
            $notices = [];
            foreach ($notice as $item) {
                $notices[] = [
                    'id' => $item->id,
                    'content' => $item->content,
                    'redirect_type' => $item->redirect_type,
                    'redirect_to' => $item->redirect_to,
                    'url'=>$item->url,
                    'created_at' => $item->created_at
                ];
            }
            //获取菜单
            $navData = Nav::where('status', 1)->with("image")->get();
            $nav = [];
            if ($navData->count() > 0) {
                foreach ($navData as $key=>$item) {
                    $nav[] = [
                        'id' => $item->id,
                        'image' => $item->image->file_url,
                        'name' => $item->name,
                        'redirect_type' => $item->type,
                        'redirect_to' => $item->redirect_to,
                        'url' => $item->url
                    ];
                }
            }
            //获取首页套餐广告
            $adv1 = Advs::advsImage('adv1','list_order','desc');
            //获取首页末尾广告
            $adv2 = Advs::advsImage('adv2');
            $fuHao = compareAppVersion('1.0.0',$version);
            if ($platform=='android' && compareAppVersion('1.1.3',$version)=='gt'){
                $nav = array_slice($nav,5,4);
                if ($fuHao == 'lt'){
                    unset($adv1[1],$adv1[2]);
                }else{
                    array_splice($adv1, 0, 1);
                }
            }else {
                $nav = array_slice($nav,0,5);
                if ($fuHao == 'lt' || $platform == 'weixin') {
                    array_splice($nav, 2, 1);
                    unset($adv1[1], $adv1[2]);
                } else {
                    unset($nav[3]);
                    array_splice($adv1, 0, 1);
                }
            }
            //热门好物
            $hotGoods = Goods::where("hot",1)->where('is_onsale',1)->limit(4)->select('id', 'goods_name','header', 'image', 'sell_price', 'original_price', 'sales_volume', 'detail')->get();
            $goods = [];
            if ($hotGoods->count()>0){
                foreach ($hotGoods as $item){
                    $imageUrl = (Attachments::find($item->header))->file_url;
                    $goods[] = [
                        "id" => $item->id,
                        "goods_name" => $item->goods_name,
                        "image" => $imageUrl,
                        "sell_price" => $item->sell_price,
                        "original_pirce" => $item->original_price,
                        "sales_volume" => $item->sales_volume,
                        "store_num" => $item->store_num,
                        "url" => self::PATH.'api/shop/detail/'.$item->id
                    ];
                }
            }
            //首页套餐4
            $oilRechargeMeal = Recharge::where("is_home_show",1)->orderBy('discount','asc')->select("id","title","discount","max_amount","min_amount","only_first","months","tag","sell_num","selected","img_url","amount")->limit(4)->get();
            $newMeal = [];
            if ($oilRechargeMeal->count()>0){
                foreach ($oilRechargeMeal as $item) {
                    $money = $item->amount;
                    $item->save_money = $money * $item->months - $money * $item->discount * $item->months;
                    array_push($newMeal, $item);
                }
            }
            //直充套餐2
            $oilRechargeZhiChong = array_slice(config('recharge.meal'),0,2);
            $zhichong = [];
            if (count($oilRechargeZhiChong)>0){
                foreach ($oilRechargeZhiChong as $item) {
                    $item['save_money'] = $item['sell_price'] - $item['real_price'];
                    array_push($zhichong, $item);
                }
            }
            return success(["banner" => $banner,'hot_goods'=>$goods, "notice" => $notices, "menu" => $nav, "adv1" => $adv1, "adv2" => $adv2,'shop_image_open'=>(int)Configure::getByKey('shop_image_open'),'recharge_mel'=>$oilRechargeMeal,"recharge_zhichong"=>$zhichong]);
        }catch (\Exception $e) {
            Log::error($e);
            return fail(500, "系统错误");
        }
    }


    //新闻资讯
    public static function news($page,$size,$path,$version,$platform,$bundleId){
        try{
            $newsData = News::forPage($page,$size)->with("image")->orderBy('sort','desc')->orderBy('id','desc')->get();
            $news = [];
            if ($newsData->count()>0){
                foreach ($newsData as $item){
                    $news[] = [
                        "id"=>$item->id,
                        "title"=>$item->title,
                        "image"=>$item->image->file_url,
                        "url" => $path.'api/new/detail/'.$item->id,
                        "created_at"=>date('Y-m-d H:i:s',strtotime($item->created_at))
                    ];
                }
            }
            //查询广告
            $bannerArray['discover1'] = Advs::advsImage('discover1',$bundleId);
            $bannerArray['discover2'] = Advs::advsImage('discover2',$bundleId)[0];
            $nav = Advs::advsImage('discover3',$bundleId);
            //判断版本号
            if ($platform=='ios' && $version=='2.2.7'){
                array_splice($nav,1,1);
            }else{
                unset($nav[2]);
            }
            $bannerArray['discover3'] = $nav;

            $array = ["news"=>$news,"discover"=>$bannerArray];
            return success($array);
        }catch (\Exception $e){
            Log::error($e);
            return fail(500,"系统错误");
        }
    }

    /**
     * 发现导航
     * @param null $platform
     * @param null $version
     * @return array
     */
    public static function discover($platform=null,$version=null)
    {
        $neargas = Advs::advsImage('neargas');
        if ($platform=='ios' && $version=='1.1.0'){
            $customerservice = Advs::advsImage('customerservice');
            $data = array_merge($neargas,$customerservice);
        }else{
            $carviolation = Advs::advsImage('carviolation');
            $data = array_merge($neargas,$carviolation);
        }
        return $data;
    }
}